import threading  #多线程包引入
import socket     #主要的socket包引入
import gl         #全局变量全部放入gl 包中
class ScanPort(threading.Thread):
    def __init__(self,ip,startport,endport,timeout):
        threading.Thread.__init__(self)
        self.startport = startport
        self.endport  = endport
        self.timeout = timeout
        self.ip = socket.gethostbyname(ip)
        self.lock = threading.Lock()

    def run(self):
        for i in range(self.startport,self.endport):
            self.sockets = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            self.sockets.settimeout(self.timeout) #设置超时时间
            self.ret = self.sockets.connect_ex((self.ip,i))
            if self.ret == 0:
                self.lock.acquire()
                gl.openport.append(i)
                print('OK',i)
                self.lock.release()
                
            del self.sockets,self.ret
            
            if i == self.endport:
                self.lock.acquire()
                gl.openport.append('No')
                self.lock.release()
